<?php

class TopicsController extends BaseController {

    
        protected  $layout = 'layouts.admin';
    /**
	 * Display a listing of topics
	 *
	 * @return Response
	 */
	public function getIndex()
	{
		$topics = Topic::all();
		$this->layout->content = View::make('topics.index', compact('topics'));
	}

	/**
	 * Show the form for creating a new topic
	 *
	 * @return Response
	 */
	public function getCreate()
	{
		$this->layout->content = View::make('topics.create');
	}

	/**
	 * Store a newly created topic in storage.
	 *
	 * @return Response
	 */
	public function postStore()
	{
                $topic = new Topic;
                
                if($topic->validate(Input::all())){
                    
                    $topic->setExistValues(array('title' , 'description' , 'category_id'), true);
                    $topic->save();
                }

		return Redirect::to('topics');
	}

	/**
	 * Display the specified topic.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$topic = Topic::findOrFail($id);

		return View::make('topics.show', compact('topic'));
	}

	/**
	 * Show the form for editing the specified topic.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function getEdit($id)
	{
		$topic = Topic::find($id);

		$this->layout->content = View::make('topics.edit', compact('topic'));
	}

	/**
	 * Update the specified topic in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function postUpdate($id)
	{
		$topic = Topic::findOrFail($id);
                if($topic->validate(Input::all())){
                    
                    $topic->setExistValues(array('title' , 'description' , 'category_id'), true);
                    $topic->save();
                    return Redirect::to('topics/edit/'.$id);
                }else {
                    return Redirect::to('topics')->withErrors($topic->erros );
                }
	}

	/**
	 * Remove the specified topic from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		Topic::destroy($id);

		return Redirect::route('topics.index');
	}

}
